
package com.umpay.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.umpay.dict.retcode.MerBusiEC;
import com.umpay.exception.BusinessException;

/*******************************************************************
 * description : 订单流水处理类
 * 
 * @author zhangru
 * @date 2018年1月22日 下午8:08:12
 * @version 1.0
 *******************************************************************/
@Component
public class GetSequenceHelper {
	private static final Logger log = LogManager.getLogger(GetSequenceHelper.class);
	
	@Autowired
	private SequenceUtil sequenceUtil;
	
	/**
	 * description : 生成订单流水
	 * @author zhangru
	 * @date 2018年1月22日 下午8:56:37
	 * @version 1.0
	**/
	public String createTradeNoByOrderTableName(String key, int width ,String orderTableName) throws BusinessException {
		try {
			String dbTime = TimeUtil.date("yyMMddHHmmss");
			String orderSeq = sequenceUtil.getSeq(key, width);
			String tradeNo = new StringBuilder(dbTime).append(orderSeq).append(orderTableName.substring(orderTableName.length() - 1)).toString();
			log.info(String.format("生成的订单流水tradeNo=%s", tradeNo));
			return tradeNo;
		} catch (BusinessException e) {
			log.error("生成tradeNo失败",e.getMessage());
			throw new BusinessException(MerBusiEC.SYSTEM_ERR, "生成tradeNo失败");
		} catch (Exception ex) {
			log.error("系统发生异常",ex.getMessage());
			throw new BusinessException(MerBusiEC.SYSTEM_ERR, "系统发生异常");
		}
	}
	
	/* ********************************************
	 * method name   : createTradeNoByOrderTableNameAndBusiId 
	 * description   : 生成订单流水,首位为busiId的末尾
	 * @return       : String
	 * @param        : @param key
	 * @param        : @param width
	 * @param        : @param orderTableName
	 * @param        : @param busiId
	 * @param        : @return
	 * @param        : @throws BusinessException
	 * modified      : ncc ,  2018年5月17日 下午6:18:32
	 * @see          : 
	 * ********************************************/      
	public String createTradeNoByOrderTableNameAndBusiId(String key, int width ,String orderTableName, String busiId) throws BusinessException {
		try {
			String dbTime = TimeUtil.date("yyMMddHHmmss");
			String orderSeq = sequenceUtil.getSeq(key, width);
			String tradeNo = new StringBuilder(busiId.substring(1)).append(dbTime.substring(1)).append(orderSeq).append(orderTableName.substring(orderTableName.length() - 1)).toString();
			log.info(String.format("生成的订单流水tradeNo=%s", tradeNo));
			return tradeNo;
		} catch (BusinessException e) {
			log.error("生成tradeNo失败",e.getMessage());
			throw new BusinessException(MerBusiEC.SYSTEM_ERR, "生成tradeNo失败");
		} catch (Exception ex) {
			log.error("系统发生异常",ex.getMessage());
			throw new BusinessException(MerBusiEC.SYSTEM_ERR, "系统发生异常");
		}
	}
	
	/**
	 * description : 生成机构流水
	 * @author zhangru
	 * @date 2018年1月22日 下午8:56:37
	 * @version 1.0
	 **/
	public String createTraceByInstId(String instId, int width ,String orderTableName) throws BusinessException {
		try {
			String dbTime = TimeUtil.date("yyMMddHHmmss");
			String traceSeq = sequenceUtil.getSeq(instId, width);
			String trace = new StringBuilder(dbTime).append(traceSeq).append(orderTableName.substring(orderTableName.length() - 1)).toString();
			log.info(String.format("生成的机构流水instId=%s,trace=%s", instId,trace));
			return trace;
		} catch (BusinessException e) {
			log.error("生成trace失败",e.getMessage());
			throw new BusinessException(MerBusiEC.SYSTEM_ERR, "生成trace失败");
		} catch (Exception ex) {
			log.error("系统发生异常",ex.getMessage());
			throw new BusinessException(MerBusiEC.SYSTEM_ERR, "系统发生异常");
		}
	}
	
	 /*****************************************************************
		 * createTraceByInstIdAndOrderDate:(根据orderdate计算trace). 
		 * @author wangpengbo 
		 * @param instId
		 * @param width
		 * @param orderDate
		 * @return
		 * @throws BusinessException
		 *****************************************************************/ 
		 
		public String createTraceByInstIdAndOrderDate(String instId, int width ,String orderDate) throws BusinessException {
			
			int day = 0;
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			Calendar cal = Calendar.getInstance();
			try {
				cal.setTime(format.parse(orderDate));
			} catch (ParseException e) {
				throw new BusinessException(MerBusiEC.SYSTEM_ERR,"订单日期不合法");
			}
			day = cal.get(Calendar.MONTH);
			day = day%6;
			try {
				String dbTime = TimeUtil.date("yyMMddHHmmss");
				String traceSeq = sequenceUtil.getSeq(instId, width);
				String trace = new StringBuilder(dbTime).append(traceSeq).append(day).toString();
				log.info(String.format("生成的机构流水instId=%s,trace=%s", instId,trace));
				return trace;
			} catch (BusinessException e) {
				log.error("生成trace失败",e.getMessage());
				throw new BusinessException(MerBusiEC.SYSTEM_ERR, "生成trace失败");
			} catch (Exception ex) {
				log.error("系统发生异常",ex.getMessage());
				throw new BusinessException(MerBusiEC.SYSTEM_ERR, "系统发生异常");
			}
		}
		
		/**
		 * description : 生成批次流水  12位yyMMddHHmmss+3位流水+子表表号
		 * @author zhangru
		 * @date 2018年4月20日 下午8:47:05
		 * @version 1.0
		**/
		public String createBatchNo(String key, int width, String busiDate) throws BusinessException {
			int day = getTableNo(busiDate);
			try {
				String dbTime = TimeUtil.date("yyMMddHHmmss");
				String orderSeq = sequenceUtil.getSeq(key, width);
				String batchNo = new StringBuilder(dbTime).append(orderSeq).append(day).toString();
				log.info(String.format("生成的批次流水batchNo=%s", batchNo));
				return batchNo;
			} catch (BusinessException e) {
				log.error("生成batchNo失败",e.getMessage());
				throw new BusinessException(MerBusiEC.SYSTEM_ERR, "生成batchNo失败");
			} catch (Exception ex) {
				log.error("系统发生异常",ex.getMessage());
				throw new BusinessException(MerBusiEC.SYSTEM_ERR, "系统发生异常");
			}
		}
		
		/**
		 * description : 根据日期计算月表在线表表号
		 * @author zhangru
		 * @date 2018年4月23日 下午6:08:12
		 * @version 1.0
		**/
		public int getTableNo(String busiDate) throws BusinessException {
			int day = 0;
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			Calendar cal = Calendar.getInstance();
			try {
				cal.setTime(format.parse(busiDate));
			} catch (ParseException e) {
				throw new BusinessException(MerBusiEC.SYSTEM_ERR,"订单日期不合法");
			}
			day = cal.get(Calendar.MONTH);
			day = day%6;
			return day;
		}
		
		/**
		 * description : 根据key生成订单号，规则：key+yyMMddHHmmss+ 流水（流水长度为总长度orderLength-key-时间）
		 * @author zhangru
		 * @date 2018年8月13日 下午3:09:49
		 * @version 1.0
		**/
		public String createOrderIdByKey(String key, int orderIdLength) throws BusinessException {
			try {
				String dbTime = TimeUtil.date("yyMMddHHmmss");
				int width = orderIdLength - key.length() - dbTime.length() ;
				String orderSeq = sequenceUtil.getSeq(key, width);
				String orderId = new StringBuilder(key).append(dbTime).append(orderSeq).toString();
				log.info(String.format("生成的订单号orderId=%s", orderId));
				return orderId;
			} catch (BusinessException e) {
				log.error("生成orderId失败",e.getMessage());
				throw new BusinessException(MerBusiEC.SYSTEM_ERR, "生成orderId失败");
			} catch (Exception ex) {
				log.error("系统发生异常",ex.getMessage());
				throw new BusinessException(MerBusiEC.SYSTEM_ERR, "系统发生异常");
			}
		}
		
	public static void main(String[] args) {
		System.out.println(TimeUtil.date("yyMMddHHmmss").substring(1));
		System.out.println("02".substring(1));
	}
	
}
